<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/27
 * Time: 10:56
 */

namespace app\api\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;

class Shopping extends Common
{
    private static $result_data = array(
        'code' => '0',
        'msg' => 'Parameter error',
        'data' => []
    );
    private  $uid = '';  //用户id

    public function __construct()
    {
        parent::__construct();
        if(Session::get('uid')){
            $this->uid = Session::get('uid');
            return;
        }

        $getData = input('get.');
        if(empty($getData['uid'])){
            echo '参数错误,uid为空';exit;
        }
        if(empty($getData['sKey'])){
            echo '参数错误,sKey为空';exit;
        }
        $u_where = [
            'uid'=>$getData['uid'],
            'S_key'=>$getData['sKey'],
        ];
        $u = Db::table('role_info')->where($u_where)->field('uid')->find();
        if(empty($u)){
            echo '参数错误';exit;
        }
        $this->uid = $getData['uid'];
        Session::set('uid',$getData['uid']);
    }

    /**
     * 商城主页
     */
    public function index(){
        //搜索我的积分
        $coin = Db::table('shop_coin_info')->where('uid',$this->uid)->find();
        //搜商品
        $shop_info = Db::table('shop_info')->select();

        $this->assign('coin',$coin);
        $this->assign('shop_info',$shop_info);
        return $this->fetch();
    }

    /**
     * 兑换页面
     */
    public function duihuan(){
        if (Request::instance()->isPost()) {
            $postData = input('post.');
            if(!preg_match("/^1[34578]\d{9}$/", $postData['phone'])){
                self::$result_data['msg'] = '手机号码格式错误';
                return json_encode(self::$result_data);
            }
            //检查姓名 预留手机号 是否实名注册
            $uinfo = Db::table('account_info')->where('uid',$this->uid)->find();
            if(empty($uinfo)){
                self::$result_data['msg'] = '用户不存在';
                return json_encode(self::$result_data);
            }
            if($uinfo['is_real'] != 1){
                self::$result_data['msg'] = '兑换失败，用户未实名认证';
                return json_encode(self::$result_data);
            }
            if($uinfo['RealName'] != $postData['name']){
                self::$result_data['msg'] = '兑换失败，认证的姓名错误';
                return json_encode(self::$result_data);
            }
            if($uinfo['MobilePhone'] != $postData['phone']){
                self::$result_data['msg'] = '兑换失败，认证的手机号错误';
                return json_encode(self::$result_data);
            }
            if($uinfo['ID_card_no'] != $postData['card']){
                self::$result_data['msg'] = '兑换失败，认证的身份证号错误';
                return json_encode(self::$result_data);
            }
            //积分是否够
            $coin = Db::table('shop_coin_info')->where('uid',$this->uid)->find();
            $shop_info = Db::table('shop_info')->where('serial',$postData['id'])->find();
            if($coin['coin'] < $shop_info['coin']){
                self::$result_data['msg'] = '兑换失败，积分不够';
                return json_encode(self::$result_data);
            }

            //通过后插入
            $insertData = [
                'shop_uid'=>$this->uid,
                'shop_code'=>$shop_info['shop_code'],
                'address'=>$postData['isOld']=='true' ? $postData['oldAddress'] : $postData['shen'].$postData['shi'].$postData['qu'].$postData['address'],
                'MobilePhone'=>$postData['phone'],
                'ID_card_no'=>$postData['card'],
            ];
            Db::table('shop_record')->insert($insertData);
            /*//插入后扣分 插入日志
            $insertData = [
                'uid'=>$this->uid,
                'chage_coin'=>$shop_info['coin'],
                'coin_ing'=>$coin['coin'] - $shop_info['coin'],
                'chage_reason'=>"-".$shop_info['serial'],
            ];
            Db::table('shop_coin_record')->insert($insertData);
            //更新shop_coin_info已使用的积分
            $updateData = [
                'use_coin'=>$coin['use_coin'] + $shop_info['coin'],
                'coin'=>$coin['coin'] - $shop_info['coin']
            ];
            Db::table('shop_coin_info')->where('uid',$this->uid)->update($updateData);*/
            $res = Db::execute('call setShopCoin('.$this->uid.','."-".$shop_info['coin'].','."-".$shop_info['serial'].')');
            if($res){
                self::$result_data['code'] = 200;
            }else{
                self::$result_data['msg'] = '积分不足';
            }


            return json_encode(self::$result_data);

        }else{
            $getData = input('get.');
            $shop_info = Db::table('shop_info')->where('serial',$getData['id'])->find();
            //搜索我的积分
            $coin = Db::table('shop_coin_info')->where('uid',$this->uid)->find();
            //上次收货地址
            $oldAddress = Db::table('shop_record')->where('shop_uid',$this->uid)->order('serial desc')->find();

            if(empty($shop_info)){
                echo '商品不存在';exit;
            }else{
                $this->assign('shop_info',$shop_info);
                $this->assign('coin',$coin);
                $this->assign('oldAddress',$oldAddress ? $oldAddress['address'] : '');
            }
            return $this->fetch();
        }
    }

    /**
     * 订单列表
     */
    public function orderlist(){
        //搜索我的积分
        $coin = Db::table('shop_coin_info')->where('uid',$this->uid)->find();
        $where = [];
        $where['r.shop_uid'] = $this->uid;
        //搜索我的订单列表
        $shop_record = Db::table('shop_record')
            ->alias('r')
            ->where($where)
            ->join('(shop_info i)','i.shop_code = r.shop_code','LEFT')
            ->join('(account_info a)','a.uid = r.shop_uid','LEFT')
            ->field('r.*,i.coin,i.shop_name,i.img_path,a.RealName')
            ->order('r.serial desc')
            ->select();

        $this->assign('coin',$coin);
        $this->assign('shop_record',$shop_record);
        return $this->fetch();
    }
}